package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.Result;
import com.ruoyi.common.utils.ResultUtil;
import com.ruoyi.system.domain.SysTraining;
import com.ruoyi.system.domain.param.SysTrainingPageParam;
import com.ruoyi.system.domain.po.SysTrainingPo;
import com.ruoyi.system.domain.vo.SysTrainingVo;
import com.ruoyi.system.service.ISysTrainingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/***
 *@title SysShopController
 *@description <TODO 培训管理——控制层>
 *@author 然子哥
 *@version 1.0.0
 *@create 2023/5/18 10:01
 **/
@RestController
@RequestMapping("/training")
@Api(tags = "培训列表")
public class SysTrainingController
{

    private final ISysTrainingService trainingService;

    public SysTrainingController(ISysTrainingService trainingService)
    {
        this.trainingService = trainingService;
    }


    /**
     * TODO 查询培训列表
     * @author 然子哥
     * @date 2023/5/18 10:12
     * @return com.system.labor.core.base.result.R
     */
    @GetMapping("list")
    @ApiOperation(value = "培训列表-不分页")
    public Result<List<SysTraining>> list()
    {
        //QueryWrapper<SysTraining> queryWrapper = new QueryWrapper<>();
        //queryWrapper.orderByDesc("uploadTime");
        //return ResultUtil.data(trainingService.list(queryWrapper));
        return ResultUtil.data(trainingService.getList());
    }


    /**
     * TODO 分页查询
     * @author 然子哥
     * @date 2023/5/18 10:2
     * @return com.system.labor.core.base.result.R
     */
    @PostMapping("/page")
    @ApiOperation(value = "培训列表-分页")
    public Result<IPage<SysTrainingVo>> listPage(@RequestBody SysTrainingPageParam param)
    {
        return ResultUtil.data(trainingService.select(param));

    }

    @PostMapping("/check")
    @ApiOperation(value = "培训列表-校验同一项目下文件名称不可重复")
    public Result<Boolean> check(@RequestBody SysTrainingPo param)
    {
        return ResultUtil.status(trainingService.check(param.getFileName(),param.getProjectId(),param.getId()));
    }


    @PostMapping("/submit")
    @ApiOperation(value = "培训列表-新增")
    public Result<Boolean> save(@RequestParam("fileName") String fileName,
                  @RequestParam("description")String description,
                  @RequestParam("projectId")Integer projectId,
                  @RequestParam("trainingCategoryId")Integer trainingCategoryId,
                  @RequestParam("file") MultipartFile file)
    {
        return ResultUtil.status(trainingService.submit(fileName,description,projectId,trainingCategoryId,file));
    }
    @PostMapping("/sort")
    @ApiOperation(value = "培训列表-排序")
    public Result<Boolean> sort(@RequestParam("id") Integer id,
                                @RequestParam("sort")Integer sort)
    {
        return ResultUtil.status(trainingService.sort(id,sort));
    }
    /**
     * TODO 根据id回显培训信息
     * @author 然子哥
     * @date 2023/5/23 13:51
     * @param id 培训文件id
     * @return com.system.labor.core.base.result.R
     */
    @GetMapping("get/{id}")
    @ApiOperation(value = "查看详情")
    public Result<SysTrainingVo> getById(@PathVariable Integer id)
    {
        return ResultUtil.data(trainingService.selectById(id));
    }

    /**
     * @author 然子哥
     * @date 2023/5/23 11:0
     * @return com.system.labor.core.base.result.R
     */
    @PostMapping("/update")
    @ApiOperation(value = "培训列表-修改")
    public Result<Boolean> updateById(@RequestParam("id") Integer id,
                  @RequestParam("fileName") String fileName,
                  @RequestParam("description")String description,
                  @RequestParam("projectId")Integer projectId,
                  @RequestParam("trainingCategoryId")Integer trainingCategoryId,
                  @RequestParam(value = "file",required = false) MultipartFile file)
    {
        return ResultUtil.status(trainingService.updateTraining(id,fileName,description,projectId,trainingCategoryId,file));
    }

    /**
     * @author 然子哥
     * @date 2023/5/23 10:22
     * @param id 文件id
     * @return com.system.labor.core.base.result.R
     */
    @GetMapping("/remove/{id}")
    @ApiOperation(value = "培训列表-删除")
    public Result<Boolean> removeById(@PathVariable Integer id)
    {
        return ResultUtil.status(trainingService.updateIsDeleted(id));
    }

}
